package com.quasar.hpatient.module.doctor;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.quasar.hpatient.R;
import com.quasar.hpatient.module.doctor_search.DoctorSearchActivity;

import lib.quasar.base.frame.BaseFragment;
import lib.quasar.widget.bar.ContactBar;
import lib.quasar.widget.menu.NavigationView;

/**
 * description: 我的医生
 * created by kalu on 2018/3/19 16:14
 */
public class DoctorFragment extends BaseFragment<DoctorPresenter> implements DoctorView {

    @Override
    public int initView() {
        return R.layout.fragment_doctor;
    }

    @Override
    public void initDataLocal() {

        final NavigationView menu = getView().findViewById(R.id.fragment_doctor_menu);
        menu.setOnMenuChangeListener(() -> goNext(DoctorSearchActivity.class));

        final RecyclerView recycler = getView().findViewById(R.id.fragment_doctor_recycler);
        getPresenter().initList(this, recycler);

        ContactBar side = getView().findViewById(R.id.fragment_mydoctor_sidebar);
        if (null != side) {
            side.setOnBarChangeListener(letter -> {

                DoctorPresenter presenter = getPresenter();
                presenter.scrollRecycler(DoctorFragment.this, letter);
            });
        }
    }

    @Override
    public void initDataNet() {

        final RecyclerView recycler = getView().findViewById(R.id.fragment_doctor_recycler);
        final ContactBar side = getView().findViewById(R.id.fragment_mydoctor_sidebar);
        getPresenter().doctorList(this, recycler, side);
    }

    @Override
    public void scrollBar(String letter) {
        ContactBar side = getView().findViewById(R.id.fragment_mydoctor_sidebar);
        if (null == side) return;

        side.scrollLetter(letter);
    }

    @Override
    public void scrollRecycler(int position) {

        if (null == getView()) return;
        final RecyclerView recycler = getView().findViewById(R.id.fragment_doctor_recycler);
        if (null == recycler) return;

        RecyclerView.LayoutManager layoutManager = recycler.getLayoutManager();
        if (null == layoutManager || !(layoutManager instanceof LinearLayoutManager)) return;

        int firstVisibleItemPosition = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
        if (firstVisibleItemPosition == position) return;

        layoutManager.scrollToPosition(position);
    }
}